package com.xkcoding.https.controller;

/**
 * Created with IntelliJ IDEA.
 * User: liuyaowen
 * Date: 2017/1/12
 * Time: 8:48
 *
 * @Description: AjAX返回对象
 */


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

public class ResultBean<T> {
	private static final Logger logger = LogManager.getLogger(ResultBean.class);
	// 返回状态码 (默认 00:成功,9999:失败)
	public static final  String SUCCESS = "00";

	private static final  String SUCCESS_MESSAGE = "success";

	private String code = SUCCESS;

	// 返回的中文消息
	private String message;

	// 成功返回对象数据
	private T model;

	// 成功返回列表数据
	private List<T> list;

	public ResultBean(T model) {
		this.model = model;
		this.code = SUCCESS;
		this.message = SUCCESS_MESSAGE;
	}

	public ResultBean(List<T> list) {
		this.list = list;
		this.code = SUCCESS;
		this.message = SUCCESS_MESSAGE;
	}

	public ResultBean(String code, String message, T model) {
		this.code = code;
		this.message = message;
		this.model = model;
	}

	public ResultBean(boolean code, String message) {
		this.code = code ? SUCCESS : "9999";
		this.message = message;
	}

	public ResultBean(boolean code, T model) {
		this.code = code ? SUCCESS : "9999";
		this.message = null;
		this.model = model;
	}

	public ResultBean() {
		this.code = SUCCESS;
		this.message = SUCCESS_MESSAGE;
	}





	public String getCode() {
		return code;
	}

	public ResultBean setCode(String code) {
		this.code = code;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public ResultBean setMessage(String message) {
		this.message = message;
		return this;
	}

	public Object getModel() {
		return model;
	}

	public ResultBean setModel(T model) {
		this.model = model;
		return this;
	}

	public ResultBean setList(List<T> list) {
		this.list = list;
		return this;
	}

	public List<T> getList() {
		return list;
	}

	public ResultBean addSuccess(String message) {
		this.message = message;
		this.code = SUCCESS;
		this.model = null;
		return this;
	}

	public ResultBean addError(String message) {
		this.message = message;
		this.code = "9999";
		this.model = null;
		return this;
	}

	public ResultBean addError(String code, T model) {
		this.message = "调用失败";
		this.code = code;
		this.model = model;
		return this;
	}

	public ResultBean addError(String code, String message) {
		this.message = message;
		this.code = code;
		this.model = null;
		return this;
	}

	public ResultBean success(T model) {
		this.message = SUCCESS_MESSAGE;
		this.model = model;
		this.code = SUCCESS;
		return this;
	}


}
